package com.ifreeee.springview.redir;

import java.io.File;
import java.util.HashSet;
import java.util.Set;

public class ReDir {
    // C:/Users/Klaus/Videos/沈念如-龙族
    private static final String ROOT_DIR_STR = "C:/Users/Klaus/Videos/沈念如龙族/4";

    //
    private static final int ZERO_NUM = 3;

    //
    private static final String APPEND_CHAR = "0";

    //
    private static int numIdx = 0;

    public static void main(String[] args) throws Exception {

//        reDir();
        reName();


    }

    public static void reDir() throws Exception {
        File rootDir = new File(ROOT_DIR_STR);
        // 1、2、3、4
        File[] files = rootDir.listFiles();

        for (File file : files) {
            File[] files1 = file.listFiles();
            for (File file1 : files1) {
                File[] files2 = file1.listFiles();
                for (File file2 : files2) {
                    File newFile = new File(file, file2.getName());
                    boolean b = file2.renameTo(newFile);
                    System.out.println(file2.getAbsolutePath() + "||||" + newFile.getAbsolutePath());
                    file1.delete();
                }

            }
        }
    }

    public static void reName() throws Exception {
        Set<Character> numSet = new HashSet<>();
        numSet.add('0');
        numSet.add('1');
        numSet.add('2');
        numSet.add('3');
        numSet.add('4');
        numSet.add('5');
        numSet.add('6');
        numSet.add('7');
        numSet.add('8');
        numSet.add('9');


        StringBuilder pathStr = new StringBuilder();
        StringBuilder numStr = new StringBuilder();
        File rootDir = new File(ROOT_DIR_STR);
        // 1、2、3、4
        // File[] files = rootDir.listFiles();
        File[] accs = rootDir.listFiles();

        // for (File file : files) {
            // File[] accs = file.listFiles();
            for (File acc : accs) {
                numIdx = 0;
                String name = acc.getName();
                String dir = acc.getParent();

                pathStr.delete(0, pathStr.length());
                numStr.delete(0, numStr.length());

                pathStr.append(dir);
                pathStr.append(File.separator);

                char[] chars = name.toCharArray();
                for (char c : chars) {
                    boolean contains = numSet.contains(c);
                    // 数字加到 数字队列里
                    if (contains) {
                        numStr.append(c);
                        continue;
                    }
                    // 如果 numStr 有数字
                    fillChar(pathStr, numStr);
                    pathStr.append(c);
                }
                fillChar(pathStr, numStr);
                // System.out.println(dir + "\\" + name);
                // System.out.println(acc.getAbsolutePath() + "====" + pathStr);
                 System.out.println(acc.renameTo(new File(pathStr.toString())) + "====" + acc.getAbsolutePath() + "====" + pathStr);
            }
        // }
    }

    public static void fillChar(StringBuilder pathStr, StringBuilder numStr) {
        // 如果 numStr 有数字
        int len = numStr.length();
        if (len > 0 && len <= ZERO_NUM) {
            numIdx ++;
            if (numIdx == 2 || numIdx == 3) {
                for (int i = 0, l = ZERO_NUM - len; i < l; i++) {
                    pathStr.append(APPEND_CHAR);
                }
            }
            pathStr.append(numStr);
            numStr.delete(0, numStr.length());
        }
    }
}
